<?php





class UploadVideoBehavior extends ModelBehavior {

/******************************************************************************/

var $settings = array();

/******************************************************************************/

function setup(&$Model, $settings) {
	$this->settings[$Model->name] = (array)$settings;
}

function encode_filename($fn) {
	return Inflector::slug(pathinfo($fn, PATHINFO_BASENAME)) . '.' . Inflector::slug(pathinfo($fn, PATHINFO_EXTENSION));
}

/******************************************************************************/

function afterFind(&$Model, $results, $primary) {
	if (is_array($results)) {
		foreach ($results as $i => $result) {
			if (is_array($result) && array_key_exists($Model->name, $result) && is_array($model_data = $result[$Model->name])) {
				foreach ($this->settings[$Model->name] as $field_name => $field_params) {
					if (array_key_exists($field_name, $model_data) && is_file($filename = WWW_ROOT . 'files' . DS . $Model->table . DS . $model_data[$Model->primaryKey] . '_' . $field_name . DS . $model_data[$field_name])) {
						$url = '/files/' . $Model->table . '/' . $model_data[$Model->primaryKey] . '_' . $field_name . '/' . $model_data[$field_name];
						$results[$i][$Model->name][$field_name . '-uploaded'] = array('basename' => basename($filename), 'filename' => $filename, 'filesize' => filesize($filename), 'filemtime' => filemtime($filename), 'url' => $url, 'url_time' => $url . '?' . filemtime($filename));
					}
				}
			}
		}
	}
	return $results;
}

function rowAfterFindAssoc(&$Model, $result) {
	if (is_array($result) && array_key_exists($Model->name, $result) && is_array($results = $result[$Model->name])) {
		foreach ($results as $i => $model_data) {
			if (is_array($model_data)) {
				foreach ($this->settings[$Model->name] as $field_name => $field_params) {
					if (array_key_exists($field_name, $model_data) && is_file($filename = WWW_ROOT . 'files' . DS . $Model->table . DS . $model_data[$Model->primaryKey] . '_' . $field_name . DS . $model_data[$field_name])) {
						$url = '/files/' . $Model->table . '/' . $model_data[$Model->primaryKey] . '_' . $field_name . '/' . $model_data[$field_name];
						$result[$Model->name][$i][$field_name . '-uploaded'] = array('basename' => basename($filename), 'filename' => $filename, 'filesize' => filesize($filename), 'filemtime' => filemtime($filename), 'url' => $url, 'url_time' => $url . '?' . filemtime($filename));
					}
				}
			}
		}
	}
	return $result;
}

function rowsAfterFindAssoc(&$Model, $results) {
	if (is_array($results)) foreach ($results as $i => $result) {
		$results[$i] = $this->rowAfterFindAssoc($Model, $result);
	}
	return $results;
}

/******************************************************************************/

function _deleteVideo(&$Model, $id, $field_name) {
	foreach (glob(WWW_ROOT . 'files' . DS . $Model->table . DS . $id . '_' . $field_name . DS . '*') as $filename) {
		@unlink($filename);
		@rmdir(dirname($filename));
	}
}

function afterSave(&$Model, $created) {
	$model_data = $Model->data[$Model->name];
	foreach ($this->settings[$Model->name] as $field_name => $field_params) {
		$upload_name = 'upload_' . $field_name;
		$delete_name = 'delete_' . $field_name;
		if (array_key_exists($upload_name, $model_data) && is_array($model_data[$upload_name]) && array_key_exists('tmp_name', $model_data[$upload_name]) && is_uploaded_file($tmp_filename = $model_data[$upload_name]['tmp_name']) && $Model->hasField($field_name)) {
			// delete old
			$this->_deleteVideo($Model, $Model->id, $field_name);
			$Model->saveFieldSafe($field_name, '');
			// upload new
			$filename = WWW_ROOT . 'files' . DS . $Model->table . DS . $Model->id . '_' . $field_name . DS . $this->encode_filename(basename($model_data[$upload_name]['name']));
			if (!is_dir(dirname($filename))) @mkdir(dirname($filename), 0755, TRUE);
			if (move_uploaded_file($tmp_filename, $filename)) {
				@chmod($filename, 0644);
				$Model->saveFieldSafe($field_name, basename($filename));
			}
		} elseif (array_key_exists($delete_name, $model_data) && (bool)$model_data[$delete_name] && $Model->hasField($field_name)) {
			$this->_deleteVideo($Model, $Model->id, $field_name);
			$Model->saveFieldSafe($field_name, '');
		}
	}
}

function afterDelete(&$Model) {
	foreach ($this->settings[$Model->name] as $field_name => $field_params) {
		$this->_deleteVideo($Model, $Model->id, $field_name);
	}
}

/******************************************************************************/

}


?>